package ch.ethz.fcl.metrobuzz.controller.gui.swing;

import java.awt.Color;
import java.util.Hashtable;

import javax.swing.BorderFactory;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import net.miginfocom.swing.MigLayout;

import ch.ethz.fcl.metrobuzz.MBContainer;
import ch.ethz.fcl.metrobuzz.data.MBData;
import ch.ethz.fcl.metrobuzz.render.interchange.InterchangeRender;
import ch.ethz.fcl.metrobuzz.viewer.MBViewer;

public class TimePanel extends JPanel {
	private static final long serialVersionUID = 2725353017457613103L;

	@SuppressWarnings("unused")
	private MBContainer container;
	
	private JSlider start_time;
	private JSlider period;

	public TimePanel(MBContainer container) {
		this.container = container;
		
		this.setBackground(Color.LIGHT_GRAY);
		this.setBorder(BorderFactory.createTitledBorder("Time Controller"));
		this.setLayout(new MigLayout());

		start_time = new JSlider(JSlider.HORIZONTAL, 0, 96, 32);
		start_time.createStandardLabels(1);
		start_time.setMajorTickSpacing(24);
		start_time.setMinorTickSpacing(4);
		start_time.setPaintTicks(true);
		start_time.setPaintLabels(true);
		start_time.addChangeListener(new TimeListener());

		Hashtable<Integer, JLabel> labelTable = new Hashtable<Integer, JLabel>();
		labelTable.put(0, new JLabel("0"));
		labelTable.put(24, new JLabel("6"));
		labelTable.put(48, new JLabel("12"));
		labelTable.put(72, new JLabel("18"));
		labelTable.put(96, new JLabel("24"));
		start_time.setLabelTable(labelTable);

		period = new JSlider(JSlider.HORIZONTAL, 0, 6, 0);
		period.createStandardLabels(1);
		period.setMajorTickSpacing(1);
		period.setMinorTickSpacing(1);
		period.setPaintTicks(true);
		period.setPaintLabels(true);
		period.addChangeListener(new TimeListener());

		Hashtable<Integer, JLabel> labelTable2 = new Hashtable<Integer, JLabel>();
		labelTable2.put(0, new JLabel("15"));
		labelTable2.put(1, new JLabel("30"));
		labelTable2.put(2, new JLabel("45"));
		labelTable2.put(3, new JLabel("1h"));
		labelTable2.put(4, new JLabel("2h"));
		labelTable2.put(5, new JLabel("4h"));
		labelTable2.put(6, new JLabel("24h"));
		period.setLabelTable(labelTable2);

		this.add(new JLabel("Start Time"));
		this.add(start_time, "span");
		this.add(new JLabel("Period"));
		this.add(period, "span, grow");
	}

	class TimeListener implements ChangeListener {
		@Override
		public void stateChanged(ChangeEvent changeEvent) {
			Object source = changeEvent.getSource();
			if (source instanceof JSlider) {
				JSlider slider = (JSlider) source;
				if (slider.equals(start_time)) {
					if (period.getValue() == 6)
						MBData.startTime = 0;
					else
						MBData.startTime = slider.getValue();
				} else if (slider.equals(period)) {
					int value = slider.getValue();
					switch (value) {
					case 0:
						MBData.startTime = start_time.getValue();
						MBData.period = 1;
						break;
					case 1:
						MBData.startTime = start_time.getValue();
						MBData.period = 2;
						break;
					case 2:
						MBData.startTime = start_time.getValue();
						MBData.period = 3;
						break;
					case 3:
						MBData.startTime = start_time.getValue();
						MBData.period = 4;
						break;
					case 4:
						MBData.startTime = start_time.getValue();
						MBData.period = 8;
						break;
					case 5:
						MBData.startTime = start_time.getValue();
						MBData.period = 16;
						break;
					case 6:
						MBData.startTime = 0;
						MBData.period = MBData.ONE_DAY_TIME;
						break;
					default:
						MBData.startTime = start_time.getValue();
						MBData.period = 1;
					}
				}

				InterchangeRender.UPDATE = true;
				MBViewer.newUpdate = true;
			}
		}
	}
}
